/**
 * v-role
 * 按钮权限指令
 */
import { useAuthStore } from '@/stores/auth'

const auth = {
  mounted(el, binding) {
    const { value } = binding
    const authStore = useAuthStore()
    // admin 角色拥有所有权限
    if (authStore.identity?.roles?.includes('admin')) return

    // 判断 roles 是否严格包含 value
    if (Array.isArray(value)) {
      const hasRole = value.every((item) => authStore.identity?.roles?.includes(item))
      if (!hasRole) el.remove()
    } else {
      if (!authStore.identity?.roles?.includes(value)) el.remove()
    }
  }
}

export default auth
